-module(tools).
-author("hpj").

-include_lib("stdlib/include/qlc.hrl").

-include("emqttd.hrl").

%% API
-export([
          selection/1,
          get_unixtime/0,
          generate_rand/1,
          get_today_over_second/0,
          timestamp_to_datetime/1,
          get_appkey/0,
          to_bitstring/1,
          to_js_date/1,
          upload_url_create_pid/1
        ]).

selection([]) -> [];
selection([X|Y]) -> selection(Y, X, [], []).
 
selection([], X, [], Final) -> [X|Final];
 
selection([], Small, [X|Y], Final) ->
  selection(Y, X, [], [Small|Final]);
 
selection([X|Y], Small, Temp, _Final) ->
  case X<Small of
    true -> selection(Y, Small, [X|Temp], _Final);
    _ -> selection(Y, X, [Small|Temp], _Final)
  end.

get_unixtime() ->
  calendar:datetime_to_gregorian_seconds(calendar:local_time()) - calendar:datetime_to_gregorian_seconds({{1970,1,1}, {8,0,0}}).

generate_rand(Length) ->
  Now = os:timestamp(),
  random:seed(element(1, Now), element(2, Now), element(3, Now)),
  lists:foldl(fun(_I, Acc) -> [do_rand(0) | Acc] end, [], lists:seq(1, Length)).

do_rand(R) when R > 46, R < 58; R > 64, R < 91; R > 96 ->
  R;

do_rand(_R) ->
  do_rand(47 + random:uniform(75)).

to_js_date(Time) ->
  {datetime,{{Y,M,D},{HH,MM,SS}}} = Time,
  list_to_binary(lists:concat([Y,"-",M,"-",D," ",HH,":",MM,":",SS])).

%% 获取当天剩余的秒数
get_today_over_second() ->
  Now = get_unixtime(),
  timer:seconds((Now-((Now+28800) rem 86400)) + 86400 - Now).  

timestamp_to_datetime(undefined) ->
    <<>>;
timestamp_to_datetime(Timestamp) when is_integer(Timestamp) ->
    {{YYYY,MM,DD},{HH,SS,Mm}} = calendar:gregorian_seconds_to_datetime(Timestamp +calendar:datetime_to_gregorian_seconds({{1970,1,1}, {8,0,0}})),
    list_to_binary(lists:concat([YYYY,"-",MM,"-",DD," ",HH,":",SS,":",Mm]));    
timestamp_to_datetime(Timestamp) when is_list(Timestamp) ->
    {{YYYY,MM,DD},{HH,SS,Mm}} = calendar:gregorian_seconds_to_datetime(list_to_integer(Timestamp) +calendar:datetime_to_gregorian_seconds({{1970,1,1}, {8,0,0}})),
    list_to_binary(lists:concat([YYYY,"-",MM,"-",DD," ",HH,":",SS,":",Mm]));

timestamp_to_datetime(Timestamp) when is_binary(Timestamp) ->
    {{YYYY,MM,DD},{HH,SS,Mm}} = calendar:gregorian_seconds_to_datetime(binary_to_integer(Timestamp) +calendar:datetime_to_gregorian_seconds({{1970,1,1}, {8,0,0}})),
    list_to_binary(lists:concat([YYYY,"-",MM,"-",DD," ",HH,":",SS,":",Mm])).

get_appkey() ->
  {_,_,U} = os:timestamp(),
  list_to_binary(uuid:to_string(uuid:uuid1()) ++ "-" ++ lists:flatten(io_lib:format("~6.10.0B", [U]))).    

to_bitstring(Value) when is_list(Value) ->
  list_to_bitstring(Value);

to_bitstring(Value) when is_integer(Value) ->
  integer_to_binary(Value);

to_bitstring(Value) when is_binary(Value) ->
  Value;

to_bitstring(Value) when is_atom(Value) ->
  atom_to_binary(Value,utf8).  

upload_url_create_pid(CallBackUrl) ->
  ham_server_http_client:start_link( CallBackUrl ).
